package com.zenithmind.news.service.impl;

import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.utils.BeanCopyUtils;
import com.zenithmind.news.mapper.NewsMapper;
import com.zenithmind.news.pojo.entity.News;
import com.zenithmind.news.pojo.vo.NewsVO;
import com.zenithmind.news.service.NewsCacheService;
import com.zenithmind.news.service.NewsQueryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 新闻查询服务实现类 - 遵循单一职责原则
 * 专门负责新闻的各种查询操作
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class NewsQueryServiceImpl implements NewsQueryService {

    private final NewsMapper newsMapper;
    private final NewsCacheService newsCacheService;

    @Override
    public List<NewsVO> getHotNews(Integer limit) {
        // 先从缓存获取
        List<NewsVO> cachedHotNews = newsCacheService.getCachedHotNews();
        if (cachedHotNews != null && !cachedHotNews.isEmpty()) {
            return cachedHotNews.stream().limit(limit).toList();
        }

        List<News> hotNews = newsMapper.selectHotNews(limit);
        String currentUserId = UserContext.getUserId();
        List<NewsVO> result = hotNews.stream()
                .map(news -> convertToVO(news, currentUserId))
                .toList();
        
        // 缓存热门新闻
        newsCacheService.cacheHotNews(result, 10, TimeUnit.MINUTES);
        
        return result;
    }

    @Override
    public List<NewsVO> getRecommendNews(String userId, Integer limit) {
        List<News> recommendNews = newsMapper.selectRecommendNews(userId, limit);
        return recommendNews.stream()
                .map(news -> convertToVO(news, userId))
                .toList();
    }

    @Override
    public List<NewsVO> getTopNews(String categoryId, Integer limit) {
        List<News> topNews = newsMapper.selectTopNews(categoryId, limit);
        String currentUserId = UserContext.getUserId();
        return topNews.stream()
                .map(news -> convertToVO(news, currentUserId))
                .toList();
    }

    @Override
    public List<NewsVO> getRelatedNews(String newsId, Integer limit) {
        List<News> relatedNews = newsMapper.selectRelatedNews(newsId, limit);
        String currentUserId = UserContext.getUserId();
        return relatedNews.stream()
                .map(news -> convertToVO(news, currentUserId))
                .toList();
    }

    @Override
    public List<NewsVO> getNewsByTags(List<String> tags, Integer limit) {
        List<News> newsList = newsMapper.selectNewsByTags(tags, limit);
        String currentUserId = UserContext.getUserId();
        return newsList.stream()
                .map(news -> convertToVO(news, currentUserId))
                .toList();
    }

    @Override
    public List<NewsVO> searchNews(String keyword, Integer limit) {
        List<News> newsList = newsMapper.searchNewsByKeyword(keyword, limit);
        String currentUserId = UserContext.getUserId();
        return newsList.stream()
                .map(news -> convertToVO(news, currentUserId))
                .toList();
    }

    /**
     * 转换为VO - 简化版本，只包含基本转换
     */
    @SuppressWarnings("deprecation")
    private NewsVO convertToVO(News news, String userId) {
        NewsVO vo = BeanCopyUtils.copyBean(news, NewsVO.class);
        
        // 设置基本信息
        if (news.getType() != null) {
            vo.setTypeName(getTypeName(news.getType()));
        }
        if (news.getStatus() != null) {
            vo.setStatusName(getStatusName(news.getStatus()));
        }
        if (news.getPriority() != null) {
            vo.setPriorityName(getPriorityName(news.getPriority()));
        }
        
        return vo;
    }

    private String getTypeName(Integer type) {
        return switch (type) {
            case 1 -> "原创";
            case 2 -> "转载";
            case 3 -> "爬虫";
            default -> "未知";
        };
    }

    private String getStatusName(Integer status) {
        return switch (status) {
            case 0 -> "草稿";
            case 1 -> "已发布";
            case 2 -> "已下架";
            case 3 -> "审核中";
            case 4 -> "审核失败";
            default -> "未知";
        };
    }

    private String getPriorityName(Integer priority) {
        return switch (priority) {
            case 1 -> "低";
            case 2 -> "中";
            case 3 -> "高";
            case 4 -> "紧急";
            default -> "未知";
        };
    }
}
